/*
 * @Description: 入口文件
 * @Author: wangqi
 * @Date: 2024-08-25 12:36:04
 * @LastEditTime: 2024-09-09 11:15:45
 * @LastEditAuthor: wangqi
 */
import { join } from 'path';
import { Module, RequestMethod } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import * as dotenv from 'dotenv';
import { AppController } from './app.controller';
import { AppService } from './app.service';
// import { LoggerModule } from 'nestjs-pino';

import { User } from './entities/User';
import { Profile } from './entities/Profile';
import { Roles } from './entities/Roles';
import { Logs } from './entities/Logs';

import { Env } from './enum/env';
import { UserModule } from './user/user.module';
const envFilePath = `.env.${process.env.NODE_ENV || `development`}`;

// const loggerConfig = {
//   exclude: [{ method: RequestMethod.ALL, path: 'check' }],
//   pinoHttp: {
//     name: 'pino logger:',
//     transport: {
//       targets: [
//         process.env.NODE_ENV === 'development'
//           ? {
//               level: 'info',
//               target: 'pino-pretty',
//               options: {
//                 colorize: true,
//               },
//             }
//           : {
//               level: 'info',
//               target: 'pino-roll',
//               options: {
//                 file: join('logs', 'log.txt'),
//                 frequency: 'daily', // hourly
//                 size: '10m',
//                 mkdir: true,
//               },
//             },
//       ],
//     },
//   },
// };

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath,
      load: [() => dotenv.config({ path: '.env' })],
    }),
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get(Env.DB_HOST),
        port: configService.get(Env.DB_PORT),
        username: configService.get(Env.DB_USER),
        password: configService.get(Env.DB_PASSWORD),
        database: configService.get(Env.DB_NAME),
        entities: [User, Profile, Roles, Logs],
        synchronize: true,
        // logging: process.env.NODE_ENV === 'development',
        logging: false,
      }),
    }),
    // LoggerModule.forRoot(loggerConfig),
    UserModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
